package service

import (
	"strings"

	"gitee.com/sansaniot/ssiot-core/facade/env"
	"gitee.com/sansaniot/ssiot-core/httpmvc/service"
	"gorm.io/gorm"

	"ssforward/cmd/system"
	"ssforward/common/constant"
	"ssforward/common/dto"
	"ssforward/internal/forward/models"
	"ssforward/internal/forward/models/command"
	"ssforward/internal/forward/models/query"
)

type ForwardRule struct {
	serverRule  *models.SysForwardRule
	serverModel *models.SysForwardServer
	service.Service
}

func (d *ForwardRule) SetServerRuleModel(m *models.SysForwardRule) *ForwardRule {
	d.serverRule = m
	return d
}

func (d *ForwardRule) SetServerModel(m *models.SysForwardServer) *ForwardRule {
	d.serverModel = m
	return d
}

// 添加服务规则
func (d *ForwardRule) AddForwardRule(req *command.ForwardRuleInsertReq) (err error, forwardRule models.SysForwardRule) {
	if err = req.Generate(&forwardRule); err != nil {
		return
	}
	if err = d.serverModel.FindOne(d.serverModel, dto.MakeCondition(query.ForwardServerQuery{Id: req.ServerId})); err != nil {
		return
	}
	forwardRule.ServerType = d.serverModel.Type
	// 基本信息
	if err = d.serverRule.Add(&forwardRule); err != nil {
		return
	}
	// 清理刷新配置缓存
	keyList := env.Cache.ScanKey(constant.ForwardConfigKey + "*")
	for _, key := range keyList {
		_ = env.Cache.Del(key)
	}
	system.InitForwardCache()
	return
}

// 编辑服务规则
func (d *ForwardRule) EditForwardRule(req *command.ForwardRuleEditReq) (err error, forwardRule models.SysForwardRule) {
	if err = d.Orm.Transaction(func(tx *gorm.DB) error {
		d.serverRule.Orm = tx
		d.serverModel.Orm = tx
		if err = d.serverRule.FindOne(&forwardRule, dto.MakeCondition(query.ForwardRuleQuery{Id: req.Id})); err != nil {
			return err
		}
		modMap := req.Generate(&forwardRule)
		if err = d.serverModel.FindOne(d.serverModel, dto.MakeCondition(query.ForwardServerQuery{Id: req.ServerId})); err != nil {
			return err
		}
		forwardRule.ServerType = d.serverModel.Type
		// 基本信息
		if err = d.serverRule.Mod(&forwardRule); err != nil {
			return err
		}
		// 切换按产品/设备
		if len(modMap) > 0 {
			d.serverRule.Id = forwardRule.Id
			if err = d.serverRule.ModMap(d.serverRule, modMap); err != nil {
				return err
			}
		}
		return nil
	}); err != nil {
		return
	}

	// 清理刷新配置缓存
	keyList := env.Cache.ScanKey(constant.ForwardConfigKey + "*")
	for _, key := range keyList {
		_ = env.Cache.Del(key)
	}
	system.InitForwardCache()
	return
}

// 删除服务规则
func (d *ForwardRule) DeleteServerRule(req *command.RuleDeleteReq) (err error) {
	idList := strings.Split(strings.ReplaceAll(req.Id, " ", ""), ",")
	condition := dto.MakeCondition(query.ForwardRuleQuery{IdIn: idList})
	if len(req.ServerId) > 0 {
		condition = dto.MakeCondition(query.ForwardRuleQuery{ServerIdIn: strings.Split(strings.ReplaceAll(req.ServerId, " ", ""), ",")})
	}
	if err = d.serverRule.Delete(d.serverRule, condition); err != nil {
		return
	}
	// 清理刷新配置缓存
	keyList := env.Cache.ScanKey(constant.ForwardConfigKey + "*")
	for _, key := range keyList {
		_ = env.Cache.Del(key)
	}
	system.InitForwardCache()
	return
}
